package com.ifast.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.asset.service.TaskrecordService;
import com.ifast.asset.util.NewThreadPool;
import com.ifast.asset.util.NmapExecuteUtil;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.ifast.demo.domain.DomainDO;
import com.ifast.demo.job.FindSubJob;
import com.ifast.demo.service.DomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;

@Controller
@RequestMapping("/findSub")
public class FindSubController extends BaseController {

    @Autowired
    DomainService domainService;
    @Resource
    private NmapExecuteUtil nmapExecuteUtil;
    @Autowired
    TaskrecordService taskrecordService;
    //调用域名爆破接口
    @GetMapping("/add")
    @ResponseBody
    public Result add(String domain,Integer level,Integer type){

        FindSubJob findSubJob = new FindSubJob(domain,level,type,nmapExecuteUtil,domainService,taskrecordService);
        ExecutorService executorService = NewThreadPool.getExecutorService();
        executorService.submit(findSubJob);
        return  Result.ok();
    }
    //获取检测记录接口
    @PostMapping("/list")
    @ResponseBody
    public Result list(@RequestBody DomainDO domainDTO){

        Wrapper wrapper = new EntityWrapper<>().orderBy("id", false);
        SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (domainDTO.getId()!=null){
            wrapper.eq("id",domainDTO.getId());
        }
        if (!"".equals(domainDTO.getDomain())&&domainDTO.getDomain()!=null){
            wrapper.like("domain",domainDTO.getDomain());
        }
        if (!"".equals(domainDTO.getLevel())&&domainDTO.getLevel()!=null){
            wrapper.eq("level",domainDTO.getLevel());
        }
        try{
            if (!"".equals(domainDTO.getStartTime())&&!"".equals(domainDTO.getEndTime())&&domainDTO.getStartTime()!=null&&domainDTO.getEndTime()!=null){
                wrapper.between("time",(simdate.parse(domainDTO.getStartTime())),(simdate.parse(domainDTO.getEndTime())));
            }
        }catch (Exception e){

            e.printStackTrace();
        }
        Page<DomainDO> page = domainService.selectPage(getPage(DomainDO.class,domainDTO.getPageNum(),domainDTO.getPageSize()), wrapper);
        return Result.ok(page);
    }

    @PostMapping( "/batchRemove")
    @ResponseBody
    public Result<String>  remove(@RequestBody Integer[] ids){
        domainService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }
}
